package controller.common;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pojos.common.ReplyMessage;
import pojos.parents.ParentsInfo;
import pojos.student.StudentInfo;
import pojos.teacher.TeacherInfo;
import service.CommonUtilsService;
import utils.Md5Utils;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 公共页面控制器
 */
@Controller
public class CommonPageController {

    @Resource(name = "commonUtilsService")
    private CommonUtilsService commonUtilsService;

    /**
     * 首页
     */
    @RequestMapping(value = {"/","/home","/index"})
    public String getHomePage(){
        return "/jsp/Home.jsp";
    }

    /**
     * 管理员登录页
     */
    @RequestMapping(value = {"/admin"})
    public String getAdminLoginPage(){
        return "forward:/pages/common/Login-Admin.html";
    }

    /**
     * 用户登录页
     */
    @RequestMapping(value = {"/login"})
    public String getUserLoginPage(){
        return "forward:/pages/common/Login-User.html";
    }

    /**
     * 用户登录跳转页
     */
    @RequestMapping("/jumpPage")
    public String userLoginJump(HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession();
        Object rememberObj = session.getAttribute("RememberPswd");
        if(rememberObj != null){
            String remember = rememberObj.toString();
            if(remember.equals("YES")){

                // Cookies生成
                long cookiesTime = System.currentTimeMillis();
                String cookies = Md5Utils.getMd5(String.valueOf(cookiesTime));

                // 获取用户角色
                String role = session.getAttribute("Role").toString();

                // 获取用户ID
                int id = 0;
                Object userInfo = session.getAttribute("UserInfo");
                switch (role){
                    case "S": {
                        id = ((StudentInfo)userInfo).getId();
                        break;
                    }
                    case "T": {
                        id = ((TeacherInfo)userInfo).getId();
                        break;
                    }
                    case "P": {
                        id = ((ParentsInfo)userInfo).getId();
                        break;
                    }
                    default:break;
                }

                commonUtilsService.updateUserCookies(role,id,cookies,cookiesTime);

                Cookie cookie = new Cookie("yundao",cookies);
                cookie.setMaxAge(7 * 24 * 60 *60);
                cookie.setDomain(request.getServerName());
                cookie.setPath("/");

                response.addCookie(cookie);
            }
        }
        session.removeAttribute("RememberPswd");

        return "/jsp/Login-User-Jump.jsp";
    }

    /**
     * 忘记密码页面
     */
     @GetMapping("/forgetPassword")
     public String forgetPasswordPage(){
        return "forward:/pages/common/ForgetPassword-User.html";
     }

    /**
     * 忘记密码时修改密码界面
     */
     @GetMapping("/forgetPassword-alterPassword")
     public String changePasswordPage(HttpServletRequest request){
        HttpSession session = request.getSession();
        Object forgetObj = session.getAttribute("ForgetPassword");
        if(forgetObj == null){
            return "/forgetPassword";
        }else{
            return "forward:/pages/common/UpdatePassword-User.html";
        }
     }

    /**
     * 忘记密码时修改密码成功界面
     */
    @GetMapping("/forgetPassword-alterPassword-success")
    public String changePasswordSuccess(){
        return "forward:/pages/common/Update-Password-User-Success.html";
    }

    /**
     * 查找所有的科目信息
     */
    @PostMapping(value = {"/query/subject"})
    @ResponseBody
    public ReplyMessage querySubjectInfo(){
        return commonUtilsService.queryAllEnaSubjects();
    }

    /**
     * 根据ID查找科目信息
     */
    @PostMapping(value = {"/query/subjectById"})
    @ResponseBody
    public ReplyMessage querySubjectInfoById(Integer id){
        ReplyMessage replyMessage = new ReplyMessage();

        if(id == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("ID不能为空！");
            return replyMessage;
        }

        return commonUtilsService.querySubjectById(id);
    }

    /**
     * 教师查询课程信息
     */
    @PostMapping("/teacher/frame/queryClassInfo")
    @ResponseBody
    public ReplyMessage teacherQuerySubjectInfo(HttpServletRequest request){
        HttpSession session = request.getSession();
        TeacherInfo teacherInfo = (TeacherInfo) session.getAttribute("UserInfo");

        return commonUtilsService.teacherQueryClassInfo(teacherInfo.getId());
    }

    /**
     * 学生查询课程信息
     */
    @PostMapping(value = {"/student/frame/queryClassInfo"})
    @ResponseBody
    public ReplyMessage studentQuerySubjectInfo(HttpServletRequest request){
        HttpSession session = request.getSession();
        StudentInfo studentInfo = (StudentInfo) session.getAttribute("UserInfo");

        return commonUtilsService.studentClassInfoQuery(studentInfo.getId());
    }

    /**
     * 家长查询课程信息
     */
    @PostMapping("/parents/frame/queryClassInfo")
    @ResponseBody
    public ReplyMessage parentsQuerySubjectInfo(HttpServletRequest request){
        HttpSession session = request.getSession();
        ParentsInfo parentsInfo = (ParentsInfo) session.getAttribute("UserInfo");

        return commonUtilsService.parentsClassInfo(parentsInfo.getId());
    }
}
